#!/bin/bash

# Docker镜像的名称和标签
IMAGE_NAME="place-search"
IMAGE_TAG="latest"

# 容器名称
CONTAINER_NAME="place-search"

# 检查是否有同名容器在运行，并停止并删除它
if [ "$(docker ps -q -f name=${CONTAINER_NAME})" ]; then
    echo "Stopping and removing existing container..."
    docker stop ${CONTAINER_NAME}
    docker rm ${CONTAINER_NAME}
fi

# 构建Docker镜像
echo "Building Docker image..."
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .

# 检查是否构建成功
if [ $? -ne 0 ]; then
    echo "Failed to build Docker image"
    exit 1
fi

# 运行Docker容器
echo "Running Docker container..."
docker run -d --name ${CONTAINER_NAME} --restart=always -v $PWD/temp:/app/temp -p 8101:8080  -e TZ="Asia/Shanghai" ${IMAGE_NAME}:${IMAGE_TAG}

# 检查是否运行成功
if [ $? -ne 0 ]; then
    echo "Failed to run Docker container"
    exit 1
fi

echo "Docker container is running"

# 可选：显示正在运行的容器
docker ps
